package com.eqdd.yiqidian.ui.gongneng.renshi.train;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.EmploymentListResultBean;
import com.eqdd.yiqidian.bean.rebuild.TeacherListBean;
import com.eqdd.yiqidian.bean.rebuild.TrainPlanInfoBean;
import com.eqdd.yiqidian.bean.rebuild.TrainPlanListBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditVEnterBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.test.ExpandBean;
import com.eqdd.yiqidian.test.SelectExpandActivity;
import com.eqdd.yiqidian.ui.base.EditTextActivity;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.select.SelectSingleStaffActivity;
import com.eqdd.yiqidian.ui.select.SelectTeacherActivity;
import com.eqdd.yiqidian.utils.BottomSheetUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.web.WebActivity;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;

public class AddTrainNoticeActivity extends SlimBaseMultiEditActivity {
    private RecyclerViewCustom dataBinding;

    private TrainPlanInfoBean items;
    private TrainPlanListBean planListBean;
    private BottomSheetDialog bottomSheet;
    private String guid;
    private Bundle bundle;
    private ArrayList<AddTrainTimesActivity.RangeTimeBean> timeBeans;
    private String times;
    private ExpandBean expandBean;
    private String resGuid;


    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "培训通知");
        initTopRightText("提交", v -> {
            if (checkParams()) {
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.ADD_TRAIN_NOTICE)
                        .params("userGuid", AppConstent.user.getGuid())
                        .params("comid", AppConstent.user.getCompanyId())
                        .params("thePlanId", planListBean.getId())
                        .params("theplace", setParam(4))
                        .params("teacherGuid", guid)
                        .params("teacherName", items.getTeacherName())
                        .params("teacherInfo", setParam(6))
                        .params("aSyllabus", setParam(7))
                        .params("presetReleaseTime", "")
                        .params("theTrainTime", times)
                        .params("hostdepId", expandBean.getId())
                        .params("hostdepName", expandBean.getLabel())
                        .params("respoPerson", resGuid)
                        .params("respoPersonName", setParam(10))
                        .execute(new DialogCallBack<HttpResult>(this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    isSave = false;
                                    clearData();
                                    setResult(Config.REFRESH);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort(msg);
            }
        });
    }


    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 0:
                bundle = new Bundle();
                bundle.putBoolean(Config.IS_SELECT, true);
                startActivityForResult(TrainPlanListActivity.class, bundle, Config.TRAIN_PLAN_LIST);
                break;
            case 3:
                bundle = new Bundle();
                bundle.putParcelableArrayList(Config.LIST, timeBeans);
                startActivityForResult(AddTrainTimesActivity.class, bundle, Config.ADD_TRAIN_TIMES);
                break;
//            case 2:
//                startActivityForResult(SelectPostMultiActivity.class, null, Config.SELECT_POST_MULTI);
//                break;
//            case 4:
//                startActivityForResult(SelectSingleStaffActivity.class, null, Config.SELECT_SINGLE_STAFF);
//                break;
            case 5:
                if (bottomSheet == null) {
                    bottomSheet = BottomSheetUtil.getBottomSheet(this, "", (recyclerView, position, v) -> {
                        bottomSheet.dismiss();
                        guid = "";
                        switch (position) {
                            case 0:
                                startActivityForResult(SelectTeacherActivity.class, null, Config.SELECT_TEACHER);
                                break;
                            case 1:
                                startActivityForResult(SelectSingleStaffActivity.class, null, Config.SELECT_SINGLE_STAFF);
                                break;
                            case 2:
                                SlimEditEnterBean slimEditEnterBean = (SlimEditEnterBean) slimAdapterEx.getDataItem(5);
                                bundle = new Bundle();
                                bundle.putString("content", slimEditEnterBean.getContent());
                                bundle.putString("tip", TextUtils.isEmpty(slimEditEnterBean.getTip()) ? slimEditEnterBean.getTitle() : slimEditEnterBean.getTip());
                                bundle.putString("title", slimEditEnterBean.getTitle());
                                bundle.putBoolean("canBlank", slimEditEnterBean.isCanBlank());
                                AppConstent.schemes = slimEditEnterBean.getSchemes();
                                startActivityForResult(EditTextActivity.class, bundle, p);
                                break;
                        }
                    }, "在线讲师", "企业内部", "其它");
                }
                bottomSheet.show();
                break;
            case 8:
                Bundle bundle = new Bundle();
                bundle.putString(Config.TITLE, "纪律要求");
                bundle.putString(Config.URL, "https://www.eqidd.com/html/peixun.html");
                startActivity(WebActivity.class, bundle);
                break;
            case 9:
                startActivityForResult(SelectExpandActivity.class, null, Config.SELECT_EXPAND);
                break;
            case 10:
                startActivityForResult(SelectSingleStaffActivity.class, null, Config.SELECT_SINGLE_STAFF_TWO);
                break;

        }
    }

    @Override
    protected void setEditBeans() {
        isSave = false;
        isRead = false;
        datas.add(new SlimEditEnterBean("*培训主题", "", 0, true, "请选择").setNeed(true));
        datas.add(new SlimEditHintBean("*受训对象", "", 1).setNeed(true));
        datas.add(new SlimEditHintBean("*受训人数", "", 2).setNeed(true));
        datas.add(new SlimEditVEnterBean("*培训时间", "", 3, "添加").setDeal(true).setNeed(true));
        datas.add(new SlimEditEnterBean("*培训地点", "", 4, "请输入").setNeed(true));
        datas.add(new SlimEditEnterBean("*主讲老师", "", 5, true, "请选择").setNeed(true));
        datas.add(new SlimEditEnterBean("*讲师介绍", "", 6, "请输入").setNeed(true));
        datas.add(new SlimEditEnterBean("*课程大纲", "", 7, "请输入").setNeed(true));
        datas.add(new SlimEditEnterBean("纪律要求", "", 8, true, "查看"));
        datas.add(new SlimEditEnterBean("*主办部门", "", 9, true, "查看"));
        datas.add(new SlimEditEnterBean("*培训负责人", "", 10, true, "查看"));

    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Config.SUCCESS) {
            switch (requestCode) {
                case Config.TRAIN_PLAN_LIST:
                    planListBean = (TrainPlanListBean) data.getSerializableExtra(Config.BEAN);
                    OkGo.<HttpResult<TrainPlanInfoBean>>post(HttpConfig.BASE_URL + HttpConfig.TRAIN_PLAN_INFO)
                            .params("userGuid", AppConstent.user.getGuid())
                            .params("comid", AppConstent.user.getCompanyId())
                            .params("planId", planListBean.getId())
                            .execute(new DialogCallBack<HttpResult<TrainPlanInfoBean>>(AddTrainNoticeActivity.this) {
                                @Override
                                public void onSuccess(Response<HttpResult<TrainPlanInfoBean>> response) {
                                    HttpResult<TrainPlanInfoBean> httpResult = response.body();
                                    ToastUtil.showShort(httpResult.getMsg());
                                    if (httpResult.getStatus() == 200) {
                                        items = httpResult.getItems();
                                        guid = items.getTeacherGuid();
                                        String[] split = items.getTheTrainTime().split(",");
                                        ArrayList<AddTrainTimesActivity.RangeTimeBean> rangeTimeBeans = new ArrayList<>();
                                        if (split != null && split.length > 0) {
                                            for (String s : split) {
                                                String[] split1 = s.split(" ");
                                                String date = split1[0];
                                                String[] split2 = split1[1].split("~");
                                                String from = split2[0];
                                                String to = split2[1];
                                                rangeTimeBeans.add(new AddTrainTimesActivity.RangeTimeBean(date, from, to));
                                            }
                                        }
                                        timeBeans = rangeTimeBeans;
                                        times = items.getTheTrainTime();
                                        slimAdapterEx.getDataItem(0).setContent(items.getTheTheme());
                                        slimAdapterEx.getDataItem(1).setContent(items.getTheCategory());
                                        slimAdapterEx.getDataItem(2).setContent(items.getPersonNumber() + "");
                                        slimAdapterEx.getDataItem(3).setContent(items.getTheTrainTime().replace(",", "\r\n"));
                                        slimAdapterEx.getDataItem(5).setContent(items.getTeacherName());
                                        slimAdapterEx.notifyItemRangeChanged(0, 6);
                                    }
                                }
                            });
                    break;
                case Config.SELECT_TEACHER:
                    TeacherListBean teacherListBean = (TeacherListBean) data.getSerializableExtra(Config.BEAN);
                    guid = teacherListBean.getUserGuid();
                    slimAdapterEx.getDataItem(5).setContent(teacherListBean.getRealname());
                    slimAdapterEx.notifyItemChanged(5);
                    break;
                case Config.ADD_TRAIN_TIMES:
                    timeBeans = data.getParcelableArrayListExtra(Config.LIST);
                    StringBuilder timeStrs = null;
                    StringBuilder stringBuilder = null;
                    for (AddTrainTimesActivity.RangeTimeBean timeBean : timeBeans) {
                        if (stringBuilder == null) {
                            stringBuilder = new StringBuilder(timeBean.getDate() + " " + timeBean.getFrom() + "~" + timeBean.getTo());
                            timeStrs = new StringBuilder(timeBean.getDate() + " " + timeBean.getFrom() + "~" + timeBean.getTo());
                        } else {
                            stringBuilder.append(",").append(timeBean.getDate() + " " + timeBean.getFrom() + "~" + timeBean.getTo());
                            timeStrs.append("\r\n").append(timeBean.getDate() + " " + timeBean.getFrom() + "~" + timeBean.getTo());
                        }
                    }
                    times = stringBuilder.toString();
                    slimAdapterEx.getDataItem(3).setContent(timeStrs.toString());
                    slimAdapterEx.notifyItemChanged(3);
                    break;
                case Config.SELECT_EXPAND:
                    expandBean = data.getParcelableExtra(Config.BEAN);
                    slimAdapterEx.getDataItem(9).setContent(expandBean.getLabel());
                    slimAdapterEx.notifyItemChanged(9);
                    break;

            }
        } else if (requestCode == Config.SELECT_SINGLE_STAFF && resultCode == Config.INTENT_CONTENT) {
            EmploymentListResultBean staff = (EmploymentListResultBean) data.getSerializableExtra(Config.BEAN);
            guid = staff.getGuid();
            slimAdapterEx.getDataItem(5).setContent(staff.getStaffName());
            slimAdapterEx.notifyItemChanged(5);
        } else if (requestCode == Config.SELECT_SINGLE_STAFF_TWO && resultCode == Config.INTENT_CONTENT) {
            EmploymentListResultBean staff = (EmploymentListResultBean) data.getSerializableExtra(Config.BEAN);
            resGuid = staff.getGuid();
            slimAdapterEx.getDataItem(10).setContent(staff.getStaffName());
            slimAdapterEx.notifyItemChanged(10);
        }else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
